package vcp.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;

import control.customTimestamp;
import db.parking.beans.Orders;
import db.parking.beans.Subscriber;

import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.gui.forms.OrderOccasionalForm;
import vcp.gui.forms.SubscriptionFormFull;
import vcp.gui.forms.SubscriptionFormRegular;
import vcp.gui.forms.validators.DateValidator;
import vcp.gui.forms.validators.EmailValidator;
import vcp.gui.forms.validators.NumericValidator;
import vcp.gui.forms.validators.Time24HoursValidator;
import vcp.models.OrdersOccasionalOrderModel;
import vcp.models.SubscriptionModelFull;
import vcp.models.SubscriptionModelRegular;
import vcp.views.SubscriptionView;

public class SubscriptionController extends AbstractController 
implements ActionListener
{
    
	SubscriptionModelFull model1;
	SubscriptionModelRegular model2;
    //SubscriptionModel model;
    SubscriptionView view;
    
	
    @Override
	public void actionPerformed(ActionEvent e) {
		String com = e.getActionCommand();
		switch(com){
		case "full_subscription":
			CreateFullSubscriber();
			break;
		case "reg_subscription":
			CreateRegularSubscriber();
		    break;
		}
	}
	
	public void addView(Object view) {
		this.view = (SubscriptionView)view;
	}

	

	public void addModel1(SubscriptionModelFull subscriptionModel1) {
		model1 = subscriptionModel1;		
	}
	public void addModel2(SubscriptionModelRegular subscriptionModel2) {
		model2 = subscriptionModel2;		
	}
	
	
	private void CreateFullSubscriber() {
		boolean isValidForm=false;
		Subscriber bean = new Subscriber();
		NumericValidator verifyID = new NumericValidator();
		DateValidator verifyDate = new DateValidator();

		
		SubscriptionFormFull form = view.getSubscriptionForm1();
		
        if(form.getTextCID().getText().isEmpty() 
           || form.getTextCID().getText().isEmpty()
           || form.getCarNo_field().getText().isEmpty()
           )
        {
        	JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		    isValidForm = false;
		    return;
        }
        
        //Parse all data	
		String ID = form.getTextCID().getText();
		String carNo = form.getCarNo_field().getText();
		int day=form.getDateChooser_start().getJCalendar().getDayChooser().getDay();
		int month=form.getDateChooser_start().getJCalendar().getMonthChooser().getMonth()+1;
		int year=form.getDateChooser_start().getJCalendar().getYearChooser().getYear();
		String date = Integer.toString(day)+"/"+ Integer.toString(month) +"/"+ Integer.toString(year);
		String dateFormat = "dd/MM/yyyy";
		
		//Input Validity Check
		if (ID.equals("") || carNo.equals("") || Integer.parseInt(ID)<0 || !verifyID.validate(carNo)) {
			JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
			isValidForm = false;
		}

		else if (date.equals("") || !verifyDate.isThisDateValid(date, dateFormat)) {
			JOptionPane.showMessageDialog(null,"Invalid Date Entered,Please Re-Enter","Invalid Date Error", JOptionPane.ERROR_MESSAGE);
			isValidForm = false;
	   }
	   else{
			isValidForm = true;
		}
		
		if(isValidForm==true){
			 try {
				//create new Timestamp for the time the user wants to leave the lot
				Timestamp subStart=customTimestamp.makeTimestamp(year,month,day,0,0,02,02);
				bean.setCusID(ID);
				bean.setType("Full");
				bean.setCarNo(carNo);
				bean.setSubStart(subStart);
				model1.AddFullSubscriber(bean);
				
				//start the Order Controller to insert order into database	
				} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	

		}
			
		
	}
	
	private void CreateRegularSubscriber() {
		boolean isValidForm=false;
		Subscriber bean = new Subscriber();
		NumericValidator verifyID = new NumericValidator();
		Time24HoursValidator verifyTime = new Time24HoursValidator();
		DateValidator verifyDate = new DateValidator();
		EmailValidator verifyEmail = new EmailValidator();

		
		SubscriptionFormRegular form = view.getSubscriptionForm2();
        
		if(form.getTextCID().getText().isEmpty() 
           || form.getCarNo_field().getText().isEmpty()
           || form.getTextField().getText().isEmpty()
           || form.getTextField_h().getText().isEmpty()
           || form.getTextField_m().getText().isEmpty()
           )
        {
        	JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		    isValidForm = false;
		    return;
        }
        
        //Parse all data	
		String ID = form.getTextCID().getText();
		String carNo = form.getCarNo_field().getText();
		int lotNo = Integer.parseInt(form.getTextField().getText());
		int leaveHours = Integer.parseInt(form.getTextField_h().getText());
		int leaveMinutes = Integer.parseInt(form.getTextField_m().getText());
		String time = form.getTextField_h().getText()+":"+form.getTextField_m().getText(); 
		int day=form.getDateChooserStart().getJCalendar().getDayChooser().getDay();
		int month=form.getDateChooserStart().getJCalendar().getMonthChooser().getMonth()+1;
		int year=form.getDateChooserStart().getJCalendar().getYearChooser().getYear();
		String date = Integer.toString(day)+"/"+ Integer.toString(month) +"/"+ Integer.toString(year);
		String dateFormat = "dd/MM/yyyy";
		
		//Input Validity Check
		if (ID.equals("") || carNo.equals("") || Integer.parseInt(ID)<0  || !verifyID.validate(carNo)) {
			JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
			isValidForm = false;
		}
		else if (time.equals("") || !verifyTime.validate(time)) {
				JOptionPane.showMessageDialog(null,"Invalid Time Entered,Please Re-Enter","Invalid Time Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
		}
		else if (date.equals("") || !verifyDate.isThisDateValid(date, dateFormat)) {
			JOptionPane.showMessageDialog(null,"Invalid Date Entered,Please Re-Enter","Invalid Date Error", JOptionPane.ERROR_MESSAGE);
			isValidForm = false;
	   }
	   else{
			isValidForm = true;
		}
		
		if(isValidForm==true){
			 try {
				//create new Timestamp for the time the user wants to leave the lot
				Timestamp subStart=customTimestamp.makeTimestamp(year,month,day,0,0,00,00);

				DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
				time = time + ":00"; 
		        Date date1 = sdf.parse(time);            
				bean.setCusID(ID);
				bean.setType("Regular");
				bean.setCarNo(carNo);
				bean.setTmp(date1);
				bean.setLotNo(lotNo);
				bean.setSubStart(subStart);
				model2.AddRegularSubscriber(bean);
				} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	

		}
			
		
	}
	
	
	
}
